from rest_framework import serializers
from .models import PeakPrediction, HotspotArea, DispatchRecord, DispatchEffect


class PeakPredictionSerializer(serializers.ModelSerializer):
    """高峰预测序列化器"""
    
    class Meta:
        model = PeakPrediction
        fields = '__all__'


class HotspotAreaSerializer(serializers.ModelSerializer):
    """热点区域序列化器"""
    
    class Meta:
        model = HotspotArea
        fields = '__all__'


class DispatchRecordSerializer(serializers.ModelSerializer):
    """调度记录序列化器"""
    driver_name = serializers.CharField(source='driver.real_name', read_only=True)
    
    class Meta:
        model = DispatchRecord
        fields = '__all__'


class DispatchEffectSerializer(serializers.ModelSerializer):
    """调度效果序列化器"""
    driver_name = serializers.CharField(source='driver.real_name', read_only=True)
    orders_increase = serializers.SerializerMethodField()
    revenue_increase = serializers.SerializerMethodField()
    
    class Meta:
        model = DispatchEffect
        fields = '__all__'
    
    def get_orders_increase(self, obj):
        """计算订单增长"""
        return obj.orders_after - obj.orders_before
    
    def get_revenue_increase(self, obj):
        """计算收入增长"""
        return obj.revenue_after - obj.revenue_before


class DispatchRecommendationSerializer(serializers.Serializer):
    """调度推荐请求序列化器"""
    driver_id = serializers.IntegerField(help_text="司机ID")
    time_category = serializers.ChoiceField(
        choices=[
            ('morning_peak', '早高峰'),
            ('evening_peak', '晚高峰'),
            ('night_peak', '夜间高峰'),
            ('normal', '平时')
        ],
        default='morning_peak',
        help_text="时段分类"
    )


class HotspotAnalysisSerializer(serializers.Serializer):
    """热点分析请求序列化器"""
    time_category = serializers.ChoiceField(
        choices=[
            ('morning_peak', '早高峰'),
            ('evening_peak', '晚高峰'),
            ('night_peak', '夜间高峰'),
            ('normal', '平时')
        ],
        default='morning_peak',
        help_text="时段分类"
    )
    days = serializers.IntegerField(default=7, help_text="分析天数")


class DispatchEffectQuerySerializer(serializers.Serializer):
    """调度效果查询序列化器"""
    driver_id = serializers.IntegerField(required=False, help_text="司机ID")
    days = serializers.IntegerField(default=7, help_text="查询天数")
    start_date = serializers.DateField(required=False, help_text="开始日期")
    end_date = serializers.DateField(required=False, help_text="结束日期")
